#!/bin/bash
#mem_used.sh v2.0
#prg by kang @2015.04.23
Plugin="memcache_status.sh"
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/var/PROGRAM/MANAGEMENT/modules/xbash
LANG=zh_CN.GB18030
export PATH LANG


main() {
    SYSTEM=$(uname)
    case ${SYSTEM}  in
    SunOS)
        echo "unsupport Sunos"
    ;;
    Linux)
       memcache_info
    ;;
    *)
        echo "error_=Unsupport OS:${SYSTEM}!!"
        exit
    ;;
    esac
}

printout() {
      echo "connections_="
      echo "CurrItems_="
      echo "evictions_="
      echo "Usage_="
      echo "HitRate_="
      echo "DelRate_="
      echo "EvictionsNum_="
      echo "TotalNum_="
      echo "out_="
      echo "in_="
      echo "mcget_="
      echo "mcset_="
}


### functions of linux
memcache_info() {
Mods_Conf=/etc/mods.conf
    IP=$(hostname)
    case $IP in
    *inm*)
       printout
    ;;
    *)
        which xmcstat >/dev/null
        if [ $? -ne 0 ];then
           xyum --update
        fi

        ls /tmp/.mc_* >/dev/null 2>&1
        if [ $? -ne 0 ];then
            printout
        fi

        sed -n '/proc conf begin/,/proc conf end/p' $Mods_Conf|grep "memcached" >/dev/null
        if [ $? -ne 0 ];then
           printout
           exit
        fi 
        sed -n '/proc conf begin/,/proc conf end/{/:/{/memcached/p}}' $Mods_Conf| \
        sed 's/#//' | \
        sed 's/:/ /g' | \
        awk '{if ($1 == "ipdrop"){print $2} else {print $1}}' | \
        #while read port process type name url addtmp
        while read port
        do
            if [ -n $port ];then
               if [ ! -f /tmp/.mc_$port ];then
                  xmcstat --i="127.0.0.1:$port" -g=true >/tmp/.mc_$port
		  continue
               fi
	       #last time
	       eval `cat /tmp/.mc_$port`
	       L_mc_time=$mc_time
	       L_bytes_read=$bytes_read
	       L_bytes_written=$bytes_written
	       L_cmd_set=$cmd_set
	       L_cmd_get=$cmd_get
	       L_total_items=$total_items
	       L_evictions=$evictions
		
	       #now
	       mcInfo=$(xmcstat --i="127.0.0.1:$port" -g=true)
	       eval $mcInfo
		
               #time interval
               #t_int=$(awk 'BEGIN { print '$mc_time'-'$L_mc_time' }' )
	
	       #print
	       echo "connections_$port=$curr_connections"	
	       echo "CurrItems_$port=$curr_items"
               echo "Usage_$port=$(awk 'BEGIN { printf "%.2f\n",'$memused_ratio'*100 }' )"
               echo "HitRate_$port=$(awk 'BEGIN { printf "%.2f\n",'$get_hits_ratio'*100 }' )"
               echo "DelRate_$port=$(awk 'BEGIN { printf "%.2f\n",'$delete_hits_ratio'*100 }' )"
			
               #eviction
               evictions_num=$(awk 'BEGIN { print '$evictions'-'$L_evictions' }' )
               total_num=$( awk 'BEGIN { print '$total_items'-'$L_total_items' }' )
               if [ $total_num == "0" ];then
                  echo "evictions_$port=$(awk 'BEGIN { printf "%.2f\n",'$evictions_num'/100 }' )"
               else
                  echo "evictions_$port=$(awk 'BEGIN { printf "%.2f\n",'$evictions_num'/'$total_num'*100 }' )"
               fi
	       echo "EvictionsNum_$port=$evictions_num"
	       echo "TotalNum_$port=$total_num"

               #read/write bytes
               #mc_In=$(awk 'BEGIN { printf "%.2f\n",('$bytes_read'-'$L_bytes_read')*8/1024/1024/'$t_int' }' )
               mc_In=$(awk 'BEGIN { printf "%.2f\n",('$bytes_read'-'$L_bytes_read')/1024/1024 }' )
               #mc_Out=$( awk 'BEGIN { printf "%.2f\n",('$bytes_written'-'$L_bytes_written')*8/1024/1024/'$t_int' }' )		
               mc_Out=$( awk 'BEGIN { printf "%.2f\n",('$bytes_written'-'$L_bytes_written')/1024/1024 }' )		
	       echo "in_$port=$mc_In"	
	       echo "out_$port=$mc_Out"
			
	       #set/get
               mc_Set=$(awk 'BEGIN { print '$cmd_set'-'$L_cmd_set' }' )
               mc_Get=$( awk 'BEGIN { print '$cmd_get'-'$L_cmd_get' }' )
	       echo "mcget_$port=$mc_Get"				
	       echo "mcset_$port=$mc_Set"
			
               echo "$mcInfo" >/tmp/.mc_$port
            else
               printout
            fi

        done
    ;;
    esac
}

## main
main
